/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.model.L1Object;
import l1j.jrwz.server.model.L1PcInventory;
import l1j.jrwz.server.model.L1World;
import l1j.jrwz.server.model.Instance.L1ItemInstance;
import l1j.jrwz.server.model.Instance.L1NpcInstance;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.model.item.L1ItemId;

// Referenced classes of package l1j.jrwz.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來選擇清單的封包
 */
public class C_SelectList extends ClientBasePacket {

    private static final String C_SELECT_LIST = "[C] C_SelectList";

    private static Logger _log = Logger.getLogger(C_SelectList.class.getName());

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);
            L1PcInstance pc = client.getActiveChar();

            if (pc == null) {
                return;
            }
            if (pc.isDead()) {
                return;
            }
            if (pc.isGhost()) {
                return;
            }
            // アイテム毎にリクエストが来る。
            int itemObjectId = readD();
            int npcObjectId = readD();

            if (npcObjectId != 0) { // 武器的修理
                L1Object obj = L1World.getInstance().findObject(npcObjectId);
                if (obj != null) {
                    if (obj instanceof L1NpcInstance) {
                        L1NpcInstance npc = (L1NpcInstance) obj;
                        int difflocx = Math.abs(pc.getX() - npc.getX());
                        int difflocy = Math.abs(pc.getY() - npc.getY());
                        // 3格以上的距離視為無效請求
                        if (difflocx > 3 || difflocy > 3) {
                            return;
                        }
                    }
                }

                L1PcInventory pcInventory = pc.getInventory();
                L1ItemInstance item = pcInventory.getItem(itemObjectId);
                int cost = item.get_durability() * 200;
                if (!pc.getInventory().consumeItem(L1ItemId.ADENA, cost)) {
                    return;
                }
                item.set_durability(0);
                pcInventory.updateItem(item, L1PcInventory.COL_DURABILITY);
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_SELECT_LIST;
    }
}
